package com.cetc.testkafka.config;

import com.cetc.testkafka.kafkaconsumer.SimpleKafkaConsumer;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.config.KafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;

import java.util.HashMap;
import java.util.Map;

@Configuration
//@EnableKafka
@PropertySource(value = "classpath:kafka.properties", ignoreResourceNotFound = true)
public class KafkaConsumerConfig {
    @Value("${kafka.consumer.servers}")
    private String consumer_servers;
    @Value("${kafka.consumer.enable.auto.commit}")
    private boolean consumer_enableAutoCommit;
    @Value("${kafka.consumer.session.timeout}")
    private String consumer_sessionTimeout;
    @Value("${kafka.consumer.auto.commit.interval}")
    private String consumer_autoCommitInterval;
    @Value("${kafka.consumer.group.id}")
    private String consumer_groupId;
    @Value("${kafka.consumer.auto.offset.reset}")
    private String consumer_autoOffsetReset;
    @Value("${kafka.consumer.concurrency}")
    private int consumer_concurrency;

    // 2
    @Value("${kafka.consumer2.servers}")
    private String consumer2_servers;
    @Value("${kafka.consumer2.enable.auto.commit}")
    private boolean consumer2_enableAutoCommit;
    @Value("${kafka.consumer2.session.timeout}")
    private String consumer2_sessionTimeout;
    @Value("${kafka.consumer2.auto.commit.interval}")
    private String consumer2_autoCommitInterval;
    @Value("${kafka.consumer2.group.id}")
    private String consumer2_groupId;
    @Value("${kafka.consumer2.auto.offset.reset}")
    private String consumer2_autoOffsetReset;
    @Value("${kafka.consumer2.concurrency}")
    private int consumer2_concurrency;

    public Map<String, Object> consumerConfigs() {
        Map<String, Object> propsMap = new HashMap<>();
        propsMap.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, consumer_servers);
        propsMap.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, consumer_enableAutoCommit);
        propsMap.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, consumer_autoCommitInterval);
        propsMap.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, consumer_sessionTimeout);
        propsMap.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        propsMap.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        //propsMap.put(ConsumerConfig.GROUP_ID_CONFIG, consumer_groupId); // groupId可以在这边配置，或者也可以在SimpleKafkaConsumer的@KafkaListener里面配置
        //propsMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, consumer_autoOffsetReset);
        return propsMap;
    }

    @Bean
    public ConsumerFactory<String, String> consumerFactory() {
        Map<String, Object> properties = consumerConfigs();
        //properties.put(ConsumerConfig.GROUP_ID_CONFIG, consumer_groupId);
        return new DefaultKafkaConsumerFactory<>(properties);
        //return new DefaultKafkaConsumerFactory<>(consumerConfigs());
    }

    @Bean
    public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        //factory.setConsumerFactory(consumerFactory);
        //factory.setConcurrency(consumer_concurrency);
        factory.getContainerProperties().setPollTimeout(1500);
        return factory;
    }


    //2
     public Map<String, Object> consumerConfigs2() {
        Map<String, Object> propsMap = new HashMap<>();
        propsMap.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, consumer2_servers);
        propsMap.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, consumer2_enableAutoCommit);
        propsMap.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, consumer2_autoCommitInterval);
        propsMap.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, consumer2_sessionTimeout);
        propsMap.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        propsMap.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        //propsMap.put(ConsumerConfig.GROUP_ID_CONFIG, consumer2_groupId);  // groupId可以在这边配置，或者也可以在SimpleKafkaConsumer的@KafkaListener里面配置
        propsMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, consumer2_autoOffsetReset);
        return propsMap;
    }

    @Bean
    public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> kafkaListenerContainerFactory2() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory2());
        //factory.setConcurrency(consumer2_concurrency);
        factory.getContainerProperties().setPollTimeout(1500);
        return factory;
    }

    public ConsumerFactory<String, String> consumerFactory2() {
        return new DefaultKafkaConsumerFactory<>(consumerConfigs2());
    }


    /*@Bean
    public Listener listener() {
        return new Listener();
    }*/

    //消息监听器
    /*@Bean
    public SimpleKafkaConsumer listen() {
        return new SimpleKafkaConsumer();
    }*/
}
